<?php
namespace app\app\controller\v1;

use think\Db;
use think\Request;

class TeamController extends BaseController{

    /**
     * 显示资源列表
     * 团队列表
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $header = $request->header();
        $user = Db::name('users')->where('wx_openid','eq',$header['openid'])->where('status','eq',1)->find();
        if($user){
            $team = Db::query('select b.id,b.team_name,b.team_desc,b.total,b.type_id,b.create_time,c.username from hmp_team_spread a left join hmp_team b on a.team_id=b.id left join hmp_users c on b.user_id=c.id where a.user_id="'.$user['id'].'" and b.status=1');
            return hmp(200,'success',['data'=>$team]);
        }else{
            return hmp(402,'用户不存在');
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 创建团队
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {   
        $request = Request::instance(); 
        $header  = $request->header();
        $user = Db::name('users')->where('wx_openid','eq',$header['openid'])->where('status','eq',1)->find();
        if($user){
            $count = Db::name('team')->where('user_id','eq',$user['id'])->where('status','eq',1)->count();
            if($count >= 10){
                return hmp(200,'最多创建10个团队');
            }
            $team['type_id'] = input('post.type_id');
            $team['team_name'] = input('post.team_name');
            $team['team_desc'] = input('post.team_desc');
            $team['user_id'] = $user['id'];
            $team['create_time'] = date('Y-m-d H:i:s');
            $id = Db::name('team')->insertGetId($team);
            //加入群组
            Db::name('team_spread')->insert(array('user_id'=>$user['id'],'team_id'=>$id));
            return hmp(200,'success');
        }else{
            return hmp(402,'用户不存在');
        }
    }

    /**
     * 显示指定的资源
     * 查看团队
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $id = intval(input('id'));
        $team = Db::name('team')->where('id','eq',$id)->find();

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {
        $request = Request::instance();
        $header  = $request->header();
        $param   = $request->param();
        $id = intval(input('id'));
        $user_id = getUserId($header['openid']);
        return hmp(200,'success',$user_id);
        $team['type_id'] = $param['type_id'];
        $team['team_name'] = $param['team_name'];
        $team['team_desc'] = $param['team_desc'];
        Db::name('team')->where('id','eq',$id)->update($team);
        return hmp(200,'success');
    }

    /**
     * 删除指定资源
     * 解散群组
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $request = Request::instance();
        $header  = $request->header();
        $id = intval(input('id'));
        $user = Db::name('users')->where('wx_openid','eq',$header['openid'])->where('status','eq',1)->find();
        if($user){
            Db::name('team')->where('user_id','eq',$user['id'])->where('team_id','eq',$id)->update(array('status'=>0));
            Db::name('team_spread')->where('team_id','eq',$id)->delete();
            return hmp(200,'success');
        }else{
            return hmp(402,'用户不存在');
        }
    }

    /**
     * [Out T人]
     */
    public function Out(){
        $request = Request::instance();
        $header  = $request->header();
        $user_id = getUserId($header['openid']);
        $id = intval(input('get.user_id'));
        $team_id = intval(input('get.id'));
        if(Db::name('team')->where('user_id','eq',$user_id)->where('id','eq',$team_id)->find()){
            //成员减1
            Db::query('update hmp_team set total = total - 1 where id ='.$team_id);
            //退群
            Db::name('team_spread')->where('user_id','eq',$id)->where('team_id','eq',$team_id)->delete();
            return hmp(200,'success');
        }else{
            return hmp(402,'fail');
        }
    }


}